package com.nercel.dsj.gksales.util;

import com.nercel.dsj.gksales.model.RequestMapping;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author sunlin
 * @date 2018-11-26
 */
@Component
public class SpringUtil  implements ApplicationContextAware {

    private static ApplicationContext context = null;

    private static void setContext(ApplicationContext applicationContext) {
        context = applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        setContext(applicationContext);
    }

    // 传入线程中
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        return (T) context.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }

    // 国际化使用
    public static String getMessage(String key) {
        return context.getMessage(key, null, Locale.getDefault());
    }

    // 获取当前环境
    public static String getActiveProfile() {
        return context.getEnvironment().getActiveProfiles()[0];
    }

    public static Boolean isDevProfile() {
        return getActiveProfile().equals("dev");
    }

    /**
     * 获取所有请求映射列表
     * @param request
     * @return
     */
    public static List<RequestMapping> getRequestMapping(HttpServletRequest request) {
        List<RequestMapping> result = new ArrayList<>();//存储所有url集合
        WebApplicationContext wac = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);//获取上下文对象
        Map<String, HandlerMapping> requestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(wac, HandlerMapping.class, true, false);
        for(HandlerMapping handlerMapping : requestMappings.values()) {
            if(handlerMapping instanceof RequestMappingHandlerMapping) {
                RequestMappingHandlerMapping rmhm = (RequestMappingHandlerMapping) handlerMapping;
                Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
                for(RequestMappingInfo rmi : handlerMethods.keySet()) {
                    String requestTitle = "";
                    ApiOperation apiOperationAnnotation = handlerMethods.get(rmi).getMethod().getAnnotation(ApiOperation.class);
                    if(apiOperationAnnotation != null){
                        requestTitle = apiOperationAnnotation.value();
                    }
                    for(RequestMethod method : rmi.getMethodsCondition().getMethods()){
                        PatternsRequestCondition prc = rmi.getPatternsCondition();
                        Set<String> patterns = prc.getPatterns();
                        for (String url : patterns){
                            String pattern = method.name() + ":" + url;
                            result.add(new RequestMapping(requestTitle, method.name(), url));
                        }
                    }
                }
            }
        }
        return result;
    }




}
